package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.db.entity.Ranking;
import com.xcity.game.player.cache.PlayerCacheService;

import naga.x.App;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class ArenaListResponse extends Response {
	
	protected List<Item> list;
	
	public ArenaListResponse(int serial, List<Ranking> rankings) {
		super(serial);
		list = wrap(rankings);
	}
	
	static List<Item> wrap(List<Ranking> rankings) {
		if (rankings != null && rankings.size() > 0) {
			List<Item> list = new ArrayList<Item>(rankings.size());
			for (int i = 0; i < rankings.size(); i++) {
				list.add(new Item(rankings.get(i)));
			}
			return list;
		}
		return Collections.emptyList();
	}
	
	public List<Item> getList() {
		return list;
	}
	
	public static class Item {
		protected int rank; // 排名
		protected long id; // 玩家id
		protected String name;
		protected String icon;
		protected byte sex;
		protected int level;
		protected int score; // 积分
		public Item(Ranking rank) {
			this.rank = rank.getIndex() + 1;
			Player player = getPlayerCacheService().find(rank.getPlayerId());
			if (player != null) {
				this.id = player.getId();
				this.name = player.getName();
				this.icon = player.getIcon();
				this.sex = player.getSex();
				this.level = player.getLevel();
				this.score = (int) rank.getValue();
			}
		}
		public int getRank() {
			return rank;
		}
		public long getId() {
			return id;
		}
		public String getName() {
			return name;
		}
		public String getIcon() {
			return icon;
		}
		public byte getSex() {
			return sex;
		}
		public int getLevel() {
			return level;
		}
		public int getScore() {
			return score;
		}
	}
	
	private static PlayerCacheService service;
	private static PlayerCacheService getPlayerCacheService() {
		return service != null ? service : (service = App.getApp().getServiceManager().get(PlayerCacheService.class));
	}

}
